package hu.bme.aut.consulttime.client.components;

import hu.bme.aut.consulttime.client.UserService;
import hu.bme.aut.consulttime.client.UserServiceAsync;
import hu.bme.aut.consulttime.entitydtos.UserDTO;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;

public class ModifyUser extends DialogBox {
	private TextBox textBoxPass;
	private TextBox textBoxFname;
	private TextBox textBoxLname;
	private TextBox textBoxEmail;
	private CheckBox chckbxEmail;

	/**
	 * Create a remote service proxy to talk to the server-side service.
	 */
	private UserServiceAsync userService = GWT
			.create(UserService.class);

	public ModifyUser(final UserDTO userDTO) {
		// PopupPanel's constructor takes 'auto-hide' as its boolean parameter.
		// If this is set, the panel closes itself automatically when the user
		// clicks outside of it.
		super(true);
		FlexTable flexTable = new FlexTable();

		Label lblCreateStudent = new Label("Update User");
		flexTable.setWidget(0, 0, lblCreateStudent);

		Label lblUserName = new Label("User name");
		flexTable.setWidget(1, 0, lblUserName);

		Label uNameLabel = new Label(userDTO.getUserName());
		flexTable.setWidget(1, 1, uNameLabel);

		Label lblPassword = new Label("Password");
		flexTable.setWidget(2, 0, lblPassword);
		
//		PasswordTextBox passwordTextBox = new PasswordTextBox();
//		flexTable.setWidget(2, 1, passwordTextBox);

		textBoxPass = new TextBox();
		textBoxPass.setText("Enter New Password");
		flexTable.setWidget(2, 1, textBoxPass);

		Label lblFirstName = new Label("First name");
		flexTable.setWidget(3, 0, lblFirstName);

		textBoxFname = new TextBox();
		textBoxFname.setText(userDTO.getFirstName());
		flexTable.setWidget(3, 1, textBoxFname);

		Label lblLastName = new Label("Last name");
		flexTable.setWidget(4, 0, lblLastName);

		textBoxLname = new TextBox();
		textBoxLname.setText(userDTO.getLastName());
		flexTable.setWidget(4, 1, textBoxLname);

		Label lblEmailAddress = new Label("Email address");
		flexTable.setWidget(5, 0, lblEmailAddress);

		textBoxEmail = new TextBox();
		textBoxEmail.setText(userDTO.getEmail());
		flexTable.setWidget(5, 1, textBoxEmail);

		chckbxEmail = new CheckBox("Email notification");
		chckbxEmail.setValue(userDTO.isEmailNotification());
		flexTable.setWidget(6, 1, chckbxEmail);

		Button btnCreate = new Button("Create Student");
		btnCreate.setText("Update User");
		btnCreate.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				UserDTO modifiedUserDTO = new UserDTO(userDTO.getUserName(),
						textBoxFname.getText(), textBoxLname.getText(),
						textBoxPass.getText(), textBoxEmail.getText(),
						chckbxEmail.getValue());
				modifiedUserDTO.setUserId(userDTO.getUserId());
				
				if(modifiedUserDTO.getPassword().equals("Enter New Password")){
					modifiedUserDTO.setPassword(null);
				}

				userService.updateUser(modifiedUserDTO,
						new AsyncCallback<Boolean>() {					
							@Override
							public void onFailure(Throwable caught) {
								// Show the RPC error message to the user
								Window.alert("Remote Procedure Call - Failure");
							}

							@Override
							public void onSuccess(Boolean result) {
								if(result == true){
									Window.confirm("'"+userDTO.getUserName() + "' user modified!");
								}else{
									Window.alert("User modification failed!");
								}
								
							}
						});
			}
		});
		flexTable.setWidget(7, 1, btnCreate);

		setWidget(flexTable);

	}
}
